package com.ruoyi.back.v1

import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Ok
import com.ruoyi.app.common.PageData
import com.ruoyi.app.common.pageInfo
import com.ruoyi.back.mapper.ShareRecommendMapper
import com.ruoyi.back.mapper.ShareSettingMapper
import com.ruoyi.common.core.page.TableSupport
import io.swagger.annotations.Api
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.*
import javax.persistence.*


@Entity
@TableName("fly_share_recommend")
@Table(name = "fly_share_recommend")
@ApiModel("分享推荐")
data class ShareRecommend(

    @TableId(type = IdType.AUTO)
    @ApiModelProperty("id 更新的时候需要传递")
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null,

    @ApiModelProperty("分享推荐")
    var name: String? = null,

    @ApiModelProperty("图标")
    var icon: String? = null,

    @ApiModelProperty("链接 1.本地 2.链接")
    var type: Int? = 1,

    @ApiModelProperty("链接地址")
    var url: String? = null,
)

@Entity
@TableName("fly_share_recommend")
@Table(name = "fly_share_recommend")
@ApiModel("分享设置")
data class ShareSetting(

    @TableId(type = IdType.AUTO)
    @ApiModelProperty("id 更新的时候需要传递")
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null,

    @ApiModelProperty("分享标题")
    var title: String? = null,

    @ApiModelProperty("分享内容")
    var content: String? = null,

    @ApiModelProperty("上传图片")
    var pic: String? = null,

    @ApiModelProperty("分享链接")
    var shareUrl: String? = null,
)

@Service
class ShareRecommendService : ServiceImpl<ShareRecommendMapper, ShareRecommend>()

@Service
class ShareSettingService : ServiceImpl<ShareSettingMapper, ShareSetting>()

@RestController
@RequestMapping("/system/shareManager")
@Api(tags = ["推广分享管理相关接口"])
class ShareManagerController {

    @Autowired
    private lateinit var shareRecommendService: ShareRecommendService

    @Autowired
    private lateinit var shareSettingService: ShareSettingService

    @PostMapping("saveShareRecommend")
    @ApiOperation("保存或者修改分享推荐")
    fun saveShareRecommend(@RequestBody shareRecommend: ShareRecommend): Data<Boolean> =
        Ok("操作成功", shareRecommendService.saveOrUpdate(shareRecommend))

    @GetMapping("removeShareRecommend")
    @ApiOperation("移除分享推荐")
    fun removeShareRecommend(ids: String): Data<Boolean> =
        Ok("移除成功", shareRecommendService.removeByIds(ids.split(",")))

    @GetMapping("shareRecommendPage")
    @ApiOperation("获取分享推荐分页数据")
    fun shareRecommendPage(): PageData<ShareRecommend> =
        TableSupport.buildPageRequest().let { pageDomain ->
            shareRecommendService.page(Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong())).pageInfo()
        }

    @PostMapping("saveShareSetting")
    @ApiOperation("保存或者修改分享设置")
    fun saveShareSetting(@RequestBody shareSetting: ShareSetting) =
        Ok("操作成功", shareSettingService.saveOrUpdate(shareSetting))

    @PostMapping("getShareSetting")
    @ApiOperation("获取分享设置")
    fun getShareSetting() = Ok("获取成功", shareSettingService.getOne(KtQueryWrapper(ShareSetting::class.java)
        .last("limit 1")))

}